package com.power.sifa.approve.executor.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.plf.common.domain.YesNo;
import com.plf.common.exception.BusinessException;
import com.power.sifa.util.executor.AbstractPostExecutor;
import com.power.sifa.approve.model.Apl_admre_audit;
import com.power.sifa.approve.model.Apl_decision_record;
import com.power.sifa.approve.model.Apl_main;
import com.power.sifa.approve.service.Apl_admre_auditService;
import com.power.sifa.approve.service.Apl_decision_recordService;
import com.power.sifa.approve.service.Apl_mainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xiongzhiwen
 * @version 1.0
 * @description: TODO
 * @date 2024/4/18 17:12
 */

@Component
@Slf4j
public class HearAuditDecisionInterne extends AbstractPostExecutor {

    // 用户id
    private String userid;
    // 环节分类标识
    private String category;
    // 用户名
    private String username;
    // 单位id
    private String sectionid;
    // 单位名称
    private String sectionname;
    // 部门id
    private String deptid;
    // 部门名称
    private String deptname;
    // 处理意见
    private String remark;
    // 行政区划
    private String adid;
    // 环节名称
    private String taskname;

    @Resource
    private Apl_admre_auditService apl_admre_auditService;
    @Resource
    private Apl_mainService apl_mainService;

    @Resource
    private Apl_decision_recordService apl_decision_recordService;

    @Override
    public void init(Map<String, Object> initdata) {
        this.userid = (String) initdata.get("userid");
        this.category = (String) initdata.get("category");
        this.username = (String) initdata.get("username");
        this.sectionid = (String) initdata.get("sectionid");
        this.sectionname = (String) initdata.get("sectionname");
        this.deptid = (String) initdata.get("deptid");
        this.deptname = (String) initdata.get("deptname");
        this.remark = (String) initdata.get("remark");
        this.adid = (String) initdata.get("adid");
        this.taskname = (String) initdata.get("taskname");
    }

    @Override
    public Map<String, Object> execute() throws Exception {

        // 查询当前的
        Apl_main mainObj = this.apl_mainService.getById(super.applyid);
        Apl_admre_audit auditObj = this.apl_admre_auditService.getById(applyid);

        String faapplyid = auditObj.getApplyid();
        Map<String, Object> params = new HashMap<>(16);
        // 查询当前的决定的表单是否有数据
        List<Apl_decision_record> decisionRecords  = apl_decision_recordService.getNoDecisionSubRecords(faapplyid, super.applyid);
        if (CollectionUtil.isNotEmpty(decisionRecords)) {
            // 查询是否有协办人填写的记录
            Apl_decision_record filterRecord  = decisionRecords.stream().filter(el ->  el.getSigntype() == YesNo.NO).findAny().orElse(null);
            if (ObjectUtil.isNull(filterRecord)) {
                log.error("HearAuditDecisionInterne 协办人未填写干预行政复议活动记录表");
                throw new BusinessException("协办人未填写干预行政复议活动记录表");
            }
        } else {
            log.error("HearAuditDecisionInterne 协办人未填写干预行政复议活动记录表");
            throw new BusinessException("协办人未填写干预行政复议活动记录表");
        }
        return null;
    }
}
